import { Toast } from 'antd-mobile';
import {clsqezTop10, associationResult, associationRecommend} from '../services/associtationService';

export default {
  namespace: 'association',
  state: {
    clsqezTop10:[],
    recommend:[],
    associationResult:[],
  },
  reducers: {
    change(state, { payload }) {
      return {...state, ...payload};
    },
  },
  effects: {
    *fetchClsqezTop10({ payload }, { put, call, select }) {
      const { data } = yield call(clsqezTop10);
      yield put({
        type: 'change',
        payload: {
          clsqezTop10:data,
        },
      });
    },
    *fetchRecommend({ payload }, { put, call, select }) {
      const { data } = yield call(associationRecommend, payload.key);
      yield put({
        type: 'change',
        payload: {
          recommend:data,
        },
      });
    },
    *fetchAssociationResult({ payload }, { put, call, select }) {
      const { data } = yield call(associationResult, payload.id);
      yield put({
        type: 'change',
        payload: {
          associationResult:data,
        },
      });
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/association') {
          dispatch({ type: 'fetchClsqezTop10' });
        }
        else if (pathname === '/associationResult') {
          dispatch({ type: 'fetchAssociationResult', payload:{id:query.id} });
        }
      });
    },
  },
};
